using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
namespace buildingLib.human
{
    [Serializable()]
    public class Departments : NameValueListBase<string, string>
    {
        static Departments _list;
        public static Departments GetDepartments()
        {
            if (_list == null)
                _list = DataPortal.Fetch<Departments>(
                  new Criteria(typeof(Departments)));
            return _list;
        }

        public static void InvalidCache()
        {
            _list = null;
        }

        private Departments() { }

        #region Data Access

        private void DataPortal_Fetch(Criteria criteria)
        {
            this.RaiseListChangedEvents = false;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GGetEmployeeDepartments");
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                IsReadOnly = false;
                while (dr.Read())
                {                    
                    Add(new NameValueListBase<string, string>.
                      NameValuePair(dr.GetString("depart"), dr.GetString("depart")));
                }
                IsReadOnly = true;
            }
            this.RaiseListChangedEvents = true;
        }

        #endregion
    }
}
